<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary" size="mini" class="btn_add" @click="addPermission(0,1)">添加权限</el-button>
      <el-table :data="List" style="width: 100%" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" width="width" />
        <el-table-column prop="code" label="标识" width="width" />
        <el-table-column prop="description" label="描述" width="width" />
        <el-table-column label="操作" width="width">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermission(row.id,2)">添加</el-button>
            <el-button type="text" size="mini" @click="editePermission(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="`${showText}权限点`" :visible="dialogVisible" @close="btnCancle">
      <el-form ref="addPer" :model="formData" label-width="80px" :rules="rules">
        <el-form-item label="权限名称" label-width="120px" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" label-width="120px" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" label-width="120px" prop="description">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启" label-width="120px" prop="enVisible">
          <el-switch v-model="formData.enVisible" size="mini" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-row justify="center" type="flex">
          <el-button type="primary" size="mini" @click="btnOK">确认</el-button>
          <el-button size="mini" @click="btnCancle">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, updatePermission, getPermission, delPermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      List: [],
      dialogVisible: false,
      formData: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: '', // 类型
        pid: '', // 父节点
        enVisible: 0 // 默认关闭
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let result = await getPermissionList()
              if (this.formData.id) {
                result = result.filter(item => item.id !== this.formData.id)
              }
              if (result.some(item => item.name === value)) {
                callback(new Error('该部门已经存在'))
              } else {
                callback()
              }
            } }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let result = await getPermissionList()
              if (this.formData.id) {
                result = result.filter(item => item.id !== this.formData.id)
              }
              if (result.some(item => item.code === value)) {
                callback(new Error('该部门标识已经存在'))
              } else {
                callback()
              }
            }
          }
        ]
      }
    }
  },
  // 判断编辑 添加场景
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.List = transListToTreeData(await getPermissionList(), 0)
      // console.log(getPermissionList())
    },
    // 提交按钮
    btnOK() {
      this.$refs.addPer.validate(async isOK => { // 校验是否通过
        if (this.formData.id) {
          return updatePermission(this.formData)
        }
        if (isOK) {
          await addPermission(this.formData)
          this.$message.success('部门添加成功')
          this.btnCancle()
          this.getPermissionList()
        }
      })
    },
    // 取消提交 清除表单
    btnCancle() {
      this.formData = {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: '', // 类型
        pid: '', // 父节点
        enVisible: 0 // 默认关闭
      }
      this.$refs.addPer.resetFields()
      this.dialogVisible = false
    },
    // 添加权限
    addPermission(pid, type) { // 获取按钮父级 类型
      this.formData.pid = pid
      this.formData.type = type
      this.dialogVisible = true
    },
    // 编辑权限
    async editePermission(id) {
      // console.log(id)
      this.formData = await getPermission(id)
      this.dialogVisible = true
    },
    // 删除权限
    async delPermission(id) {
      try {
        await this.$confirm('是否删除该权限')
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除权限成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

  <style>
.btn_add{
  margin: 10px;
  text-align: center;
}
  </style>
